<template>
    <div class="list">
      <ul>
        <li class="item" v-for="item of letters"
        v-bind:key="item"
        v-on:click="handleLetterClick"
        v-on:touchstart="handleTouchStart"
        v-on:touchmove="handleTouchMove"
        v-on:touchend="handleTouchEnd"
        v-bind:ref="item">
        {{item}}</li>
        <!-- <li class="item" v-for="(item, key) of cities"
        v-bind:key="key"
        v-on:click="handleLetterClick"
        v-on:touchstart="handleTouchStart"
        v-on:touchmove="handleTouchMove"
        v-on:touchend="handleTouchEnd">
        {{key}}</li> -->
      </ul>
    </div>
</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: {
      type: Object
    }
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        // console.log(i)
        letters.push(i)
      }
      // console.log(letters)
      return letters
    }
  },
  methods: {
    handleLetterClick (ele) {
      this.$emit('change', ele.target.innerText)
      // console.log(ele.target.innerText)
    },
    handleTouchStart () {
      this.touchStatus = true
    },
    handleTouchMove (ele) {
      if (this.touchStatus) {
        // const startY = this.$refs['A'][0].offsetTop
        // console.log(startY)
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = ele.touches[0].clientY - 120
          // console.log(touchY)
          const index = Math.floor((touchY - this.startY) / 20)
          // console.log(index)
          if (index >= 0 && index < this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 12)
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .list
    display flex
    flex-direction column
    justify-content center
    position absolute
    top 2.43rem
    right 0
    bottom 0
    width .4rem
    // background red
    .item
      line-height .4rem
      color $bgColor
      text-align center
</style>
